package com.swm.julie.rest.sausage.index.excel;

import java.io.File;
import java.io.IOException;

import jxl.Cell;
import jxl.CellType;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import org.junit.Assert;
import org.junit.Test;

import com.swm.julie.rest.sausage.index.SausageIndexer;
import com.swm.julie.rest.sausage.index.field.Product;

public class ExcelReader {
	
	@Test
	public void testExcelReader()
	{
		System.out.println("ExcelReader.readFile()");
		inputFile = "Book1.xls";
		try {
			read();
		} catch (BiffException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private String inputFile;

	/**
	 * read a sample .xls file contains social commerce information,
	 * then call the sausage indexer to create index files in the appointed directory
	 * @throws BiffException
	 * @throws IOException
	 */
	public void read() throws BiffException, IOException
	{
		File inputWorkbook = new File(inputFile);
		Workbook w;
		
		//Get workbook from the file
		w = Workbook.getWorkbook(inputWorkbook);
		Assert.assertNotNull(w);
		
		SausageIndexer indexer = new SausageIndexer();
		
		//Loop for getting sheet from the workbook
		for(Sheet sheet : w.getSheets())
		{
			//Loop for columns of the sheet
			for(int row = 0; row < sheet.getRows(); ++row)
			{
				Product product = new Product();
				
				for(int col = 0; col < sheet.getColumns(); ++col)
				{
					Cell cell = sheet.getCell(col, row);
//					CellType cellType = cell.getType();
					
					/**
					 * column set : 
					 * 0 company
					 * 1 itemName
					 * 2 image
					 * 3 url
					 * 4 item
					 * 5 locate	
					 * 6 discountRatio
					 * 7 price
					 * 8 shopName
					 * 9 shopPhone
					 * 10 shopLocate
					 * 11 buyStart
					 * 12 buyEnd
					 * 13 useStart
					 * 14 useEnd
					 **/
					switch(col)
					{
					case 0 : product.setCompany(cell.getContents()); break;
					case 1 : product.setItemName(cell.getContents()); break;
					case 2 : product.setImage(cell.getContents()); break;
					case 3 : product.setUrl(cell.getContents()); break;
					case 4 : product.setItem(cell.getContents()); break;
					case 5 : product.setLocate(cell.getContents()); break;
					case 6 : 
						{
							CellType cellType = cell.getType();
							if(cellType == CellType.NUMBER) product.setDiscountRatio(Double.valueOf(cell.getContents()));
							else product.setDiscountRatio(0); break;
						}
					case 7 : product.setPrice(cell.getContents()); break;
					case 8 : product.setShopName(cell.getContents()); break;
					case 9 : product.setShopPhone(cell.getContents()); break;
					case 10 : product.setShopLocate(cell.getContents()); break;
					case 11 : product.setBuyStart(cell.getContents()); break;
					case 12 : product.setBuyEnd(cell.getContents()); break;
					case 13 : product.setUseStart(cell.getContents()); break;
					case 14 : product.setUseEnd(cell.getContents()); break;
					}
					
				}
				Assert.assertNotNull("product instance", product);
				//call the indexer
				indexer.indexingProduct(product);
				/**
				 * Desktop에서는 D드라이브에 index 폴더가 생성되어 indexing됨
				 */
			}
		}
		
		indexer.close();
		
	}
	
	public void setInputFile(String inputFile) 
	{
		this.inputFile = inputFile;
	}
	
}
